<template>
	<view class="content">
		<!-- <view class="order-tab">
			<view class="tab-item" :class="{on: type === item.id}" @click="typeclick(item.id, item.name)" v-for="(item,index) in nav" :key="index">{{item.name}}</view>
		</view> -->
		
		<view class="ex-list" v-show="type === 1">
			<view class="item" v-for="item,index in list" :key="index">
				<image class="img" :src="baseUrl + item.image.source" mode="widthFix"></image>
				
				<view class="name">
					{{item.name}}
				</view>
				<view class="money">积分：{{item.price}}</view>
				<view class="buy-btn" @click="buyNow(item.skuId)">去兑换</view>
			</view>
		</view>
		
		<view class="list" v-show="type === 2">
			<view class="item" :style="{background:'url('+ img +') center no-repeat',backgroundSize:'cover'}" v-for="(item,index) in list1" :key="index" :item-id="index">
				<view class="left">
					<view class="t1">
						<text>￥</text>{{item.reduction}}
					</view>
					<view class="t2">
						{{item.name}}
					</view>
				</view>
				<view class="right">
					<view class="t1">
						{{item.reduction}}{{item.reduction>0?'元':''}}优惠券
					</view>
					<view class="t2">
						{{item.beginDate | ymd}} 至 {{item.endDate | ymd}}
					</view>
					
					<view class="btn" @click="goEx(item.id)">
						去兑换
					</view>
					<view class="bview">
						<view class="point">需{{item.point}}积分兑换</view>
						<view class="cate" v-if="item.storeProductCategory !== ''"> {{item.storeProductCategory.name}}</view>
					</view>
				</view>
				<!-- <view class="bq">
					未使用
				</view> -->
			</view>
		</view>
	</view>
</template>

<script>
	import {
		couponCodeEx, getCouponList
	} from '@/api/user.js'
	import {
		productPage
	} from '@/api/product.js'
	import {
		checkout
	} from '@/api/order.js'
	export default {
		filters: {
			ymd(val) {
				if(val !== '') {
					return val.split(' ')[0]
				} else {
					return ''
				}
			}
		},
		data() {
			return {
				nav: [
					{
						id: 1,
						name: '积分商城',
					},
					{
						id: 2,
						name: '优惠券',
					}
				],
				type: 1,
				page: 1,
				list: [],
				list1: [],
				baseUrl: this.$baseUrl,
				img: '../../static/images/coupon.png'
			}
		},
		onShow() {
			this.page = 1
			this.list = []
			this.init()
		},
		onReachBottom() {
			this.page++
			uni.showLoading({
				title: '加载中...'
			})
			setTimeout(() => {
				if(this.type === 1) {
					this.init()
				} else {
					this.initData()
				}
				uni.hideLoading()
			}, 800)
		},
		methods: {
			typeclick(type, name) {
				this.page = 1
				this.list = []
				this.list1 = []
				this.type = type
				uni.setNavigationBarTitle({
					title: name
				})
				if(type === 1) {
					this.init()
				} else {
					this.initData()
				}
			},
			init() {
				productPage({pageNo: this.page, pageSize: 10, type: 'EXCHANGE'}).then(res => {
					res.data.page.list.forEach(r => {
						this.list.push(r)
					})
				})
			},
			initData() {
				getCouponList({type: 'EXCHANGE', pageNumber: this.page}).then(res => {
					res.data.page.forEach(r => {
						this.list1.push(r)
					})
				})
			},
			buyNow(skuId) {
				checkout({quantity: 1, skuId}).then(res => {
					if(res && res.data) {
						uni.navigateTo({
							url: '/pages/pay/pay?quantity=1&skuId=' + skuId
						})
					}
				})
			},
			goEx(couponId) {
				couponCodeEx({couponId}).then(res => {
					if(res && res.message) {
						uni.showToast({
							title: res.message,
							icon: 'none'
						})
						
						setTimeout(() => {
							uni.navigateTo({
								url: '/pages/coupon/coupon'
							})
						}, 800)
					}
				})
			},
		}
	}
</script>

<style lang="scss">
page {
	background-color: #f5f5f5;
}
.order-tab {
		display: flex;
		align-items: center;
		justify-content: space-evenly;
		padding: 0 30rpx;

		.tab-item {
			position: relative;
			padding: 35rpx 0;
			font-size: 24rpx;
		}

		.tab-item.on::before {
			position: absolute;
			content: '';
			width: 100%;
			height: 5rpx;
			bottom: 0;
			background-color: #b81c22;
		}
}

.ex-list {
				display: flex;
				align-items: center;
				justify-content: space-between;
				flex-wrap: wrap;

				.item {
					width: 50%;
					text-align: center;
					margin-bottom: 40rpx;
					padding: 20rpx;
					.img {
						margin-bottom: 20rpx;
						height: 170rpx;
					}

					.name {
						margin-bottom: 15rpx;
					}

					.money {
						margin-bottom: 20rpx;
					}

					.buy-btn {
						width: 160rpx;
						height: 48rpx;
						line-height: 44rpx;
						text-align: center;
						border-radius: 44rpx;
						background-color: #C10016;
						color: #fff;
						font-size: 28rpx;
						margin: auto;
					}
				}
}

.list {
		padding: 70rpx 30rpx;

		.item {
			width: 100%;
			background-repeat: no-repeat;
			background-size: 100% 100%!important;
			background-color: #fff;
			padding: 35rpx 0;
			display: flex;
			align-items: center;
			position: relative;
			overflow: hidden;
			margin-bottom: 40rpx;

			.left {
				width: 30%;
				text-align: center;

				.t1 {
					font-size: 45rpx;
					font-weight: bold;
					letter-spacing: 0px;
					color: #c10016;

					text {
						font-weight: normal;
						font-size: 30rpx;
						color: #C10016;
					}
				}

				.t2 {
					font-size: 26rpx;
					padding-top: 20rpx;
					line-height: 1.3;
					// white-space: nowrap;
				}
			}

			.right {
				padding: 0 5%;
				width: 70%;
				color: #595757;

				.t1 {

					font-size: 32rpx;

				}

				.t2 {
					font-size: 24rpx;
					padding: 15rpx 0 10rpx 0;
					line-height: 1.3;
				}
				
				.point {
					color: #C10016;
					padding-bottom: 15rpx;
				}

				.btn {
					font-size: 28rpx;
					width: 160rpx;
					text-align: center;
					height: 50rpx;
					line-height: 50rpx;
					background-color: #c10016;
					border-radius: 22rpx;
					color: #ffffff;
				}
			}

			.bq {
				position: absolute;
				top: 15%;
				right: -6%;
				line-height: 30rpx;
				background: #C10016;
				color: #ffffff;
				font-size: 14rpx;
				text-align: center;
				transform: rotate(45deg);
				width: 180rpx;
			}
		}

		.item.used {
			opacity: 0.4;
		}

		.item.exc {
			opacity: 0.4;

			.left {
				.t1 {
					color: #b2b2b2;

					text {
						color: #b2b2b2;
					}
				}

				.t2 {
					color: #b2b2b2;
				}
			}

			.right {
				color: #b2b2b2;

				.btn {
					background: #b2b2b2;
				}
			}

			.bq {
				background: #b2b2b2;
			}
		}
}

.bview {
	display: flex;
	padding: 20rpx 0 0;
	margin-top: 18rpx;
	border-top: 1px solid #ededed;
	font-size: 28rpx;
	.cate {
		margin-left: 10rpx;
		color: #C10016;
	}
}
</style>
